#pragma once

#include "components.h"
#include "event.h"
#include "models.h"

#include "qbe/src/ecs/ecs.hpp"

#include "qlib/all.h"
#include "../../state/state.hpp"
#include "../../message/message.hpp"

namespace qbe::FilletTool {
class Arc {

 public:
  Arc(const qlib::Vec2d& center, const qlib::Vec2d& start_point, double central_angle);
  std::vector<qlib::Vec2d> make_arc();
  //逆时针方向为正方向
  qlib::Vec2d m_center = {0, 0};       //圆心
  qlib::Vec2d m_start_point = {0, 0};  //起始点
  double m_central_angle = 0;   //圆心角
  int m_max_points{40};
};
void exit(ecs::Query query, state::AllModeType enter_mode);
void init(entt::registry& reg, ecs::Commands commands, ecs::Query query);
void update(entt::registry& reg, ecs::Query query, ecs::Bus& bus, ecs::EventWriter<msg::RenderEvent> event);
void post(entt::registry& reg, ecs::Commands commands, ecs::Query query, ecs::Bus& bus);
void calFilet(ecs::Query query, ecs::Bus& bus, std::vector<qlib::Vec2d>& input, double r);
void sortInputPoints(ecs::Query query, std::vector<qlib::Vec2d>& input, qlib::Vec2d& inter_point, std::vector<qlib::Vec2d>& seg, bool& is_Inter);
bool pointIsSegmentInternal(qlib::Vec2d& point, qlib::Vec2d& segment_1, qlib::Vec2d& segment_2);
class FilletToolPlugin : public ecs::Plugin {
 public:
  void build(ecs::App& app) override {
    app.init_resource<FilletToolModel>(FilletToolModel{});

    app.add_systems<ecs::stage::Startup>(init);
    app.add_systems<ecs::stage::Update>(update);
    app.add_systems<ecs::stage::PostUpdate>(post);
  };
};
}  // namespace qbe::LineAddCorner